package demoMod.nanaki.vfx;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.MathUtils;
import com.megacrit.cardcrawl.characters.AbstractPlayer;
import com.megacrit.cardcrawl.core.Settings;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;
import com.megacrit.cardcrawl.vfx.AbstractGameEffect;
import com.megacrit.cardcrawl.vfx.BobEffect;
import demoMod.nanaki.NanakiMod;

public class StropheCircleEffect extends AbstractGameEffect {
    private final BobEffect bobEffect;
    private float timer;
    private final Texture bgTexture;
    private final Texture circleTexture;
    private final Texture leftTexture;
    private final Texture rightTexture;
    private boolean fading = false;

    public StropheCircleEffect(int stropheLevel) {
        bobEffect = new BobEffect(1.0F);
        bobEffect.dist = 0.02F * Gdx.graphics.getHeight();
        bgTexture = new Texture(NanakiMod.getResourcePath(String.format("stances/s%d_bg.png", stropheLevel)));
        circleTexture = new Texture(NanakiMod.getResourcePath(String.format("stances/s%d_circle.png", stropheLevel)));
        leftTexture = new Texture(NanakiMod.getResourcePath(String.format("stances/s%d_left.png", stropheLevel)));
        rightTexture = new Texture(NanakiMod.getResourcePath(String.format("stances/s%d_right.png", stropheLevel)));
        color = Color.WHITE.cpy();
        color.a = 0.0F;
        renderBehind = true;
    }

    public void dismiss() {
        fading = true;
    }

    @Override
    public void render(SpriteBatch sb) {
        Color tmpColor = color.cpy();
        tmpColor.a *= 0.3F;

        AbstractPlayer p = AbstractDungeon.player;

        sb.setBlendFunction(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
        sb.setColor(color);
        sb.draw(this.bgTexture, p.hb.cX - 544.0F, p.hb.cY - 612.0F, 544.0F, 612.0F, 1155.0F, 1149.0F, Settings.scale * 0.5F, Settings.scale * 0.5F, 0, 0, 0, 1155, 1149, false, false);

        sb.setBlendFunction(GL20.GL_SRC_ALPHA, GL20.GL_ONE);
        sb.setColor(tmpColor);
        sb.draw(this.bgTexture, p.hb.cX - 544.0F, p.hb.cY - 612.0F, 544.0F, 612.0F, 1155.0F, 1149.0F, Settings.scale * (0.5F + MathUtils.random(-0.003F, 0.003F)), Settings.scale * (0.5F + MathUtils.random(-0.003F, 0.003F)), 0, 0, 0, 1155, 1149, false, false);

        sb.setBlendFunction(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
        sb.setColor(color);
        sb.draw(this.circleTexture, p.hb.cX - 544.0F, p.hb.cY - 612.0F, 544.0F, 612.0F, 1155.0F, 1149.0F, Settings.scale * 0.5F, Settings.scale * 0.5F, -timer * 10.0F, 0, 0, 1155, 1149, false, false);

        sb.setBlendFunction(GL20.GL_SRC_ALPHA, GL20.GL_ONE);
        sb.setColor(tmpColor);
        sb.draw(this.circleTexture, p.hb.cX - 544.0F, p.hb.cY - 612.0F, 544.0F, 612.0F, 1155.0F, 1149.0F, Settings.scale * (0.5F + MathUtils.random(-0.002F, 0.002F)), Settings.scale * (0.5F + MathUtils.random(-0.002F, 0.002F)), -timer * 10.0F, 0, 0, 1155, 1149, false, false);

        sb.setBlendFunction(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
        sb.setColor(color);
        sb.draw(this.leftTexture, p.hb.cX - 544.0F, p.hb.cY - 612.0F + bobEffect.y, 544.0F, 612.0F, 1155.0F, 1149.0F, Settings.scale * 0.5F, Settings.scale * 0.5F, 0, 0, 0, 1155, 1149, false, false);

        sb.setBlendFunction(GL20.GL_SRC_ALPHA, GL20.GL_ONE);
        sb.setColor(tmpColor);
        sb.draw(this.leftTexture, p.hb.cX - 544.0F, p.hb.cY - 612.0F + bobEffect.y, 544.0F, 612.0F, 1155.0F, 1149.0F, Settings.scale * (0.5F + MathUtils.random(-0.003F, 0.003F)), Settings.scale * (0.5F + MathUtils.random(-0.003F, 0.003F)), 0, 0, 0, 1155, 1149, false, false);

        sb.setBlendFunction(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
        sb.setColor(color);
        sb.draw(this.rightTexture, p.hb.cX - 544.0F, p.hb.cY - 612.0F - bobEffect.y, 544.0F, 612.0F, 1155.0F, 1149.0F, Settings.scale * 0.5F, Settings.scale * 0.5F, 0, 0, 0, 1155, 1149, false, false);

        sb.setBlendFunction(GL20.GL_SRC_ALPHA, GL20.GL_ONE);
        sb.setColor(tmpColor);
        sb.draw(this.rightTexture, p.hb.cX - 544.0F, p.hb.cY - 612.0F - bobEffect.y, 544.0F, 612.0F, 1155.0F, 1149.0F, Settings.scale * (0.5F + MathUtils.random(-0.003F, 0.003F)), Settings.scale * (0.5F + MathUtils.random(-0.003F, 0.003F)), 0, 0, 0, 1155, 1149, false, false);

        sb.setBlendFunction(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
    }

    @Override
    public void update() {
        bobEffect.update();
        timer += Gdx.graphics.getDeltaTime();
        if (fading) {
            color.a -= Gdx.graphics.getDeltaTime() * 2.0F;
            if (color.a < 0.0F) {
                color.a = 0.0F;
                dispose();
                isDone = true;
            }
        } else {
            if (color.a < 0.99F) {
                color.a = timer * 2.0F;
                if (color.a > 1.0F) {
                    color.a = 0.99F;
                }
            }
        }
    }

    @Override
    public void dispose() {
        if (isDone) {
            return;
        }
        bgTexture.dispose();
        circleTexture.dispose();
        leftTexture.dispose();
        rightTexture.dispose();
    }
}
